//
//  JSSEmoticonToolBarView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/27.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
enum JSSEmoticonToolBartype: Int {
    
    case Recent = 0,Default,Emoji,Lxh
}


class JSSEmoticonToolBarView: UIStackView {
    
    
    //提供给外界设置当前选中按钮的index的方法
    var selectedSection: Int = 0 {
      
        didSet{
        
            let btn = viewWithTag(selectedSection) as! UIButton
            
            //print(selectedSection)
            if btn != selectedBtn {
            
                btn.selected = true
                
                selectedBtn?.selected = false
                
                selectedBtn = btn
                
            }
            
            
            
        
        }
    
    
    }
    
    //执行按钮点击事件的闭包
    var emoticonBtnClickClosure:((type:JSSEmoticonToolBartype)->())?
    
    
    //定义一个属性 用来记录btn的状态
    var selectedBtn:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func  setupUI() {
      
       //给当前JSSEmoticonToolBarView设置一个tag值 以防button的tag值跟JSSEmoticonToolBarView本身的tag值冲突
        self.tag = 30000
        
      //设置填充模式
      self.distribution = .FillEqually
        
        //添加按钮
        addchildbtn("最近", imageName: "compose_emotion_table_left", type: JSSEmoticonToolBartype.Recent)
         addchildbtn("默认", imageName: "compose_emotion_table_mid", type: JSSEmoticonToolBartype.Default)
         addchildbtn("Emoji", imageName: "compose_emotion_table_mid", type: JSSEmoticonToolBartype.Emoji)
         addchildbtn("浪小花", imageName: "compose_emotion_table_right", type: JSSEmoticonToolBartype.Lxh)

        
    }
    
    private func addchildbtn(title:String,imageName:String,type:JSSEmoticonToolBartype){
        
        let button = UIButton()
        
        button.tag = type.rawValue
        
        //添加点击事件
        button.addTarget(self, action: "didClickbtn:", forControlEvents: UIControlEvents.TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setTitle(title, forState: UIControlState.Normal)
        //设置不同状态下的文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        //设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        
        //添加到stackView里面
        addArrangedSubview(button)
        
    }
    
    //按钮的点击事件
    @objc private func didClickbtn(btn:UIButton) {
        
        
        //如果用户点的就是当前选中的按钮的话那就不执行重新记录
        if selectedBtn == btn {
            return
        }
        
        //执行闭包
        //执行闭包(当前stackview不具有监听btn点击事件的能力 所以需要用闭包把点击事件传到看外面控制器中执行)
        self.emoticonBtnClickClosure?(type: JSSEmoticonToolBartype(rawValue: btn.tag)!)
        
        //取消选中之前的按钮
        selectedBtn?.selected = false
        
        //选中当前的按钮
        btn.selected = true
        //重新记录
        selectedBtn = btn

      
        
    }

}
